package barcode.android.dao.impl;

import java.util.Calendar;

import junit.framework.Assert;
import barcode.android.activity.Barcode_androidActivity;
import barcode.android.dao.ActivityDao;
import barcode.android.dao.impl.BaseDaoImpl;
import barcode.android.dao.impl.SqliteDatabaseState;
import barcode.android.model.Activity;
import android.test.ActivityInstrumentationTestCase2;

public class ActivityDaoTest extends
		ActivityInstrumentationTestCase2<Barcode_androidActivity> {

	public ActivityDaoTest() {
		super(Barcode_androidActivity.class);
	}

	public void setUp() {
		BaseDaoImpl.initContext(getActivity().getBaseContext());
	}

	public void testAddActivity() {
		Activity org = new Activity();
		org.setEnsure1(true);
		org.setEnsure2(true);
		org.setEnsure3(false);
		org.setValidity(Calendar.getInstance());
		

		ActivityDao dao = new ActivityDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.add(org);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testQueryActivity() {
		ActivityDao dao = new ActivityDaoImpl();
		Activity org = null;
		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
//			org = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(org);
	}

	public void testDeleteActivity() {
		ActivityDao dao = new ActivityDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.delete(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);
	}

	public void testUpdateActivity() {
		Activity org = new Activity();
		org.setId(1);

		ActivityDao dao = new ActivityDaoImpl();
		boolean res = false;
		try {
			dao.beginTransaction(SqliteDatabaseState.Writable);
			res = dao.update(org);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertEquals(true, res);

		try {
			dao.beginTransaction(SqliteDatabaseState.Readable);
//			org = dao.query(1);
			dao.setTransactionSuccessful();
		} finally {
			dao.endTransaction();
		}
		Assert.assertNotNull(org);
	}
}
